package com.wssnail.sms.sender;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;
import com.wssnail.sms.properties.SmsProperties;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Author: 熟透的蜗牛
 * @CreateTime: 2022-07-12 09:39
 * @Description: 阿里云发送短信,如需要其他短信平台，可自行修改
 * @Version: 1.0
 */
@Slf4j
public class SmsSenderImpl implements SmsSender {

    //异步发送
    private AsyncClient asyncClient;
    //同步发送
    private Client synClient;

    private SmsProperties smsProperties;

    public SmsSenderImpl(AsyncClient asyncClient, Client synClient, SmsProperties smsProperties) {
        this.asyncClient = asyncClient;
        this.synClient = synClient;
        this.smsProperties = smsProperties;
    }

    @Override
    public Boolean sendSms(String smsTemplate, JSONObject object, String phone) {
        RuntimeOptions runtime = new RuntimeOptions();
        runtime.readTimeout = 5000;
        runtime.connectTimeout = 5000;
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setPhoneNumbers(phone);
        sendSmsRequest.setTemplateCode(smsTemplate);
        sendSmsRequest.setTemplateParam(object.toJSONString());
        sendSmsRequest.setSignName(smsProperties.getSignName());
        try {
            SendSmsResponse sendSmsResponse = synClient.sendSmsWithOptions(sendSmsRequest, runtime);
            log.info("发送短信的结果是>>>>>>>>>>>>>>>>>>>>>{}", sendSmsResponse);
            return (sendSmsResponse.getStatusCode() == 200) ? true : false;
        } catch (TeaException error) {
            Common.assertAsString(error.message);
            log.info("发送短信异常>>>>>>>>>>>>>>>>>>>>{}", error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            Common.assertAsString(error.message);
            log.info("发送短信异常>>>>>>>>>>>>>>>>>>>>{}", error.message);
        }
        return false;
    }


    @Override
    public Boolean asynSendSms(String smsTemplate, JSONObject object, String phone) {
        CompletableFuture<com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse> future = null;
        try {
            com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest smsRequest = com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest.builder().templateCode(smsTemplate)
                    .phoneNumbers(phone).signName(smsProperties.getSignName())
                    .templateParam(object.toJSONString()).build();
            future = asyncClient.sendSms(smsRequest);
            com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse sendSmsResponse = future.get();
            log.info("异步发送短信的结果是>>>>>>>>>>>>>{}", JSONObject.toJSONString(sendSmsResponse));
            return "OK".equals(sendSmsResponse.getBody().getCode()) ? true : false;
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return false;
    }
}
